﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class Student_Subject
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ADM_Student_Subject StudentSubject)
        {
            try
            {
                db.AddToADM_Student_Subject(StudentSubject);

                db.SaveChanges();
                return StudentSubject.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static ADM_Student_Subject Single(long id)
        {
            try
            {
                return db.ADM_Student_Subject.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<ADM_Student_Subject > All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from StudentSubject in db.ADM_Student_Subject 
                        select StudentSubject;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Test_Id")
                {
                    long Test_Id;
                    Test_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Test_Id == Test_Id);
                }
                if (item.Key == "Student_Id")
                {
                    long Student_Id;
                    Student_Id = Convert.ToInt32(item.Value);
                    query = query.Where(c => c.Student_Id == Student_Id);
                }
                if (item.Key == "Subject_Id")
                {
                    long Subject_Id;
                    Subject_Id = Convert.ToInt32(item.Value);
                    query = query.Where(c => c.Subject_Id == Subject_Id);
                }
            }
            return query.ToList();
        }

        public static void Update(ADM_Student_Subject  StudentSubject)
        {

            ADM_Student_Subject c = db.ADM_Student_Subject.Single(x => x.Id == StudentSubject.Id);
            c.Id = StudentSubject.Id;
            c.Test_Id = StudentSubject.Test_Id;
            c.Student_Id = StudentSubject.Student_Id;
            c.Subject_Id=StudentSubject.Subject_Id ;
            c.Degree = StudentSubject.Degree ;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ADM_Student_Subject.DeleteObject(db.ADM_Student_Subject.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


    }
}
